package controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pojo.Invitation;
import service.InvitationService;
import tools.Page;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.Detail;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by Administrator on 2019/1/4.
 */
@Controller
@RequestMapping("/book")
public class InvitationController {

    @Resource
    InvitationService bookService;


    @RequestMapping("/list")
    @ResponseBody
    public String getBookList(HttpServletRequest request, @RequestParam(value="chooseId",required=false)Integer chooseId,@RequestParam(value="choose",required=false)String choose, Model model){
    String pageIndex=request.getParameter("pageIndex");
        try {
            int currPageNo = 1;

            if (pageIndex == "" || pageIndex == null) {
                currPageNo = 1;
            } else {
                currPageNo = Integer.parseInt(pageIndex);
            }
            Page pageObj = new Page();
            pageObj.setCurrPageNo(currPageNo);
            pageObj.setPageSize(2);
            pageObj = bookService.getInvitationList(chooseId,choose,pageObj);
            model.addAttribute("page",pageObj);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "list";

    }

    @RequestMapping("/add")
    public String add(Detail detail, HttpServletResponse response){
        try {
            PrintWriter out = response.getWriter();
            int result = bookService.add(detail);
            if (result > 0) {
                out.print("<script type=\"text/javascript\">");
                out.print("alert(\"添加成功\");");
                out.print("</script>");
            }else{
                out.print("<script type=\"text/javascript\">");
                out.print("alert(\"添加失败\");");
                out.print("</script>");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:list";

    }
    @RequestMapping("/addBook")
    public String add(Detail detail ,Model model){
        int a=bookService.add(detail);
        if(a>0){
            return "/index.jsp";
        }

        return "addBook";

    }
    @RequestMapping(value="/update/{bookid}",method = RequestMethod.GET)
    public int modifyUserSave(@PathVariable Integer bookid, Model model){
        Invitation invitation = bookService.getBookList(bookid);
        model.addAttribute(invitation);
        return Integer.parseInt("bookmodify");
    }



}
